// pages/myeditor/myeditor.js
var util = require("../../utils/util.js")
var api = require("../../utils/api.js")
const device = wx.getSystemInfoSync()
const W = device.windowWidth
const H = device.windowHeight - 50

let cropper = require('../../utils/welCropper/welCropper.js');
var app = getApp();
Page({

    /**
     * 页面的初始数据
     */
    data: {
        Type: 1,
        sex: 0,
        selected: '',
        cover: '',
        nickName: '',
        userid: '',
        intro: '',
        userInfo: [],
        text: '',
        mobile: '',
    },
    radioChange: function(e) {
        this.setData({
            sex: e.detail.value
        })
    },
    usernameInput: function(e) {
        this.setData({
            nickName: e.detail.value
        })
    },
    usertext: function(e) {
        this.setData({
            intro: e.detail.value
        })
    },
    usermobile: function(e) {
        this.setData({
            mobile: e.detail.value
        })
    },
    /**
     * 生命周期函数--监听页面显示
     */
    onShow: function() {

        var that = this;
        wx.getStorage({
            key: 'userInfo',
            success: function(res) {
                if (res.data.avatarUrl) {
                    that.setData({
                        avatarUrl: res.data.avatarUrl
                    })
                } else {
                    wx.getUserInfo({
                        success: function(res) {
                            var userInfo = res.userInfo
                            that.setData({
                                avatarUrl: userInfo.avatarUrl
                            })
                        }
                    })
                }
                that.setData({
                    nickName: res.data.nick_name,
                    mobile: res.data.mobile,
                    userid: res.data.id,
                    sex: res.data.gender,
                })
            },
        })
        wx.showShareMenu({
            withShareTicket: true
        })
    },
    /**
     * 生命周期函数--监听页面加载
     */
    selectTap(e) {
        let that = this
        let mode = e.currentTarget.dataset.mode

        wx.chooseImage({
            count: 1,
            sizeType: ['original', 'compressed'], // 可以指定是原图还是压缩图，默认二者都有
            sourceType: ['album', 'camera'], // 可以指定来源是相册还是相机，默认二者都有
            success(res) {
                const tempFilePath = res.tempFilePaths[0]
                console.log(tempFilePath)

                // 将选取图片传入cropper，并显示cropper
                // mode=rectangle 返回图片path
                // mode=quadrangle 返回4个点的坐标，并不返回图片。这个模式需要配合后台使用，用于perspective correction
                // let modes = ["rectangle", "quadrangle"]
                // let mode = modes[1]   //rectangle, quadrangle
                that.showCropper({
                    src: tempFilePath,
                    mode: mode,
                    sizeType: ['original', 'compressed'], //'original'(default) | 'compressed'
                    maxLength: 1000, //默认2000，允许最大长宽，避免分辨率过大导致崩溃
                    callback: (res) => {
                        if (mode == 'rectangle') {
                            wx.uploadFile({
                                url: app.u.HOME_URL + app.u.UPLOADIMG, //仅为示例，非真实的接口地址
                                filePath: res,
                                name: 'file',
                                formData: {
                                    user: 'test'
                                },
                                success(res) {
                                    that.setData({
                                        cover: res.data
                                    })

                                }
                            })
                            that.hideCropper()
                        } else {
                            wx.showModal({
                                title: '',
                                content: JSON.stringify(res),
                            })
                        }

                        // that.hideCropper() //隐藏，我在项目里是点击完成就上传，所以如果回调是上传，那么隐藏掉就行了，不用previewImage
                    }
                })
            }
        })
    },
    onLoad: function(options) {
        var that = this
            // 初始化组件数据和绑定事件
        cropper.init.apply(that, [W, H]);
        wx.getStorage({
            key: 'userInfo',
            success: function(res) {
                if (res.data.cover) {
                    that.setData({
                        cover: res.data.cover
                    })
                } else {
                    wx.getUserInfo({
                        success: function(res) {
                            var userInfo = res.userInfo
                            that.setData({
                                cover: userInfo.cover
                            })
                        }
                    })
                }
                that.setData({
                    nickName: res.data.nick_name,
                    mobile: res.data.mobile,
                    userid: res.data.id,
                    sex: res.data.gender,
                    intro: res.data.intro
                })
            },
        })
    },
    headportrait: function() {
        let that = this
        wx.chooseImage({
            success(res) {
                const tempFilePaths = res.tempFilePaths
                wx.uploadFile({
                    url: app.u.HOME_URL + app.u.UPLOADIMG, //仅为示例，非真实的接口地址
                    filePath: tempFilePaths[0],
                    name: 'file',
                    formData: {
                        user: 'test'
                    },
                    success(res) {
                        that.setData({
                            cover: res.data
                        })
                    }
                })
            }
        })
    },
    confirm: function() {
        var that = this
        var myreg = /^(((13[0-9]{1})|(15[0-9]{1})|(18[0-9]{1})|(17[0-9]{1}))+\d{8})$/;
        if (that.data.nickName == '') {
            api.showError("姓名不能为空")
            return;
        }
        if (that.data.mobile == 0 || that.data.mobile == '') {
            api.showError("手机号不能为空")
            return;
        }
        if (!myreg.test(that.data.mobile)) {
            api.showError("手机号输入有误")
            return;
        }
        if (that.data.intro == 0 || that.data.intro == '') {
            api.showError("个人介绍不能为空")
            return;
        }
        if (that.data.intro.length < 6) {
            api.showError("个人介绍至少输入六个字")
            return;
        }
        var data = {
            uid: that.data.userid,
            nick_Name: that.data.nickName,
            gender: that.data.sex,
            intro: that.data.intro,
            cover: that.data.cover,
			mobile: that.data.mobile
        }
        var url = app.u.HOME_URL + app.u.ME_INFO
        api.requestUrl(data, url).then(res => {
            if (res.code) {
                api.showError('修改成功')
                setTimeout(function() {
                    wx.switchTab({
                        url: '/pages/my/my',
                    })
                }, 800)
            } else {
                api.showError(res.msg)
            }
        })
    },

})